import struct
import logging
logger = logging.getLogger('async')

def pack_seqs(seqs, lsn):
    logger.debug('bits.py -- pack_seqs with args seqs=%s, lsn=%s', seqs, lsn)
    # Input: list of sequence numbers
    string = ''
    for i in xrange(lsn + 1):
        if i in seqs:
            string = string + '0'
        else:
            string = string + '1'
    logger.debug('bits.py -- pack_seqs bitstring: %s', string)
    return pack_bits(string)

def unpack_seqs(packed):
    string = unpack_bits(packed)
    count = -1
    seqs = []
    for c in string:
        count+=1
        if c == '0':
            seqs.append(count)
    return seqs
              
def pack_bits(bitstring):
    padding_bits = 0

    bytes = []
    count = 0
    while bitstring != '':
        count += 1
        if len(bitstring) < 8:
            padding_bits = 8 - len(bitstring)
        while len(bitstring) < 8:
            bitstring = '0' + bitstring
        
        bytes.append(int(bitstring[0:8],2))
        bitstring = bitstring[8:]

    return struct.pack('hh' + str(count) + 'B', padding_bits, count, *bytes)

    
def unpack_bits(packed):
    padding, count = struct.unpack('hh', packed[:4])
    bytes = list(struct.unpack(str(count) + 'B', packed[4:]))

    def to_bits(byte):
        string = ''
        count = 0
        while byte:
            string = str(byte % 2) + string
            byte /= 2
            count += 1
        while count < 8:
            string = '0' + string
            count += 1
        return string
        
    strings = map(to_bits, bytes)
    
    if padding:
        last_index = len(strings) - 1
        last = strings[last_index]
        strings[last_index] = last[padding:]

    return ''.join(strings)

if __name__ == '__main__':
    while True:
        cmd = raw_input('>>')
        try:
            exec(cmd)
        except Exception, e:
            print e
            
        
    